#include "../../Headers/DataAccessors/advancedoptiondataaccessor.h"

AdvancedOptionDataAccessor::AdvancedOptionDataAccessor(QString databaseName):
    DB(databaseName)
{
}

QSqlTableModel * AdvancedOptionDataAccessor::getAdvancedOptions()
{
    QSqlTableModel * model = new QSqlTableModel();
    model->setTable("advancedOption");
    model->select();
    return model;
}

bool AdvancedOptionDataAccessor::setAdvancedOption(AdvancedOption option)
{
    QString values = "insert into advancedOption (name, cost) values (\""
            + option.getName() + "\", " +
            QString::number(option.getCost()) + ")";
    QSqlQuery q(db);
    if (q.exec(values))
    {
        qDebug("all ok");
        return true;
    }
    else
    {
        qDebug("something wrong");
        qDebug(values.toAscii());
        return false;
    }
}

AdvancedOption * AdvancedOptionDataAccessor::getAdvancedOption(int id)
{
    QSqlQuery query(db);
    if (query.exec("select id, name, cost from advancedOption where id = " + QString::number(id)))
    {
        while(query.next())
        {
            AdvancedOption *option = new AdvancedOption(
                        query.value(0).toInt(),//id
                        query.value(1).toString(),//name
                        query.value(2).toDouble()//cost
                        );
            return option;
        }
    }

    return NULL;
}

bool AdvancedOptionDataAccessor::deleteAdvancedOption(AdvancedOption option)
{
    QSqlQuery query(db);
    if (query.exec("delete from advancedOption where id=" + QString::number(option.getId())))
    {
        return true;
    }
    else
        return false;
}

bool AdvancedOptionDataAccessor::editAdvancedOption(AdvancedOption option)
{
    QSqlQuery query(db);
    if (query.exec("update advancedOption set name=\"" + option.getName() +
                   "\", cost=" + QString::number(option.getCost()) +
                   " where id=" + QString::number(option.getId())))
        return true;
    else
        return false;
}

QSqlTableModel * AdvancedOptionDataAccessor::getAdvancedOptionsWithIgnored(QList<AdvancedOption*> ignoredOptions)
{
    QSqlTableModel * model = new QSqlTableModel();
    model->setTable("advancedOption");
    QString filter = "";
    if (ignoredOptions.count() > 0)
    {
        QString ignored_id = "id<>" + QString::number(ignoredOptions[0]->getId());
        for(int i = 1; i < ignoredOptions.count(); i++)
            ignored_id += " and id<>" + QString::number(ignoredOptions[i]->getId());
        filter += ignored_id;
    }
    model->setFilter(filter);
    model->select();
    return model;
}

QSqlTableModel * AdvancedOptionDataAccessor::getAdvancedOptionsWithConsisted(QList<AdvancedOption *> options)
{
    QSqlTableModel * model = new QSqlTableModel();
    model->setTable("advancedOption");
    QString filter = "";
    if (options.count() > 0)
    {
        QString ignored_id = "id=" + QString::number(options[0]->getId());
        for(int i = 1; i < options.count(); i++)
            ignored_id += " or id=" + QString::number(options[i]->getId());
        filter += ignored_id;
    }
    model->setFilter(filter);
    model->select();
    return model;
}
